package com.sky.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.constant.MessageConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Setmeal;
import com.sky.exception.CategoryCreateException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.Set;

@Service
@Slf4j
@RequiredArgsConstructor
public class CategoryServiceImpl implements CategoryService {

    private final CategoryMapper categoryMapper;

    /**
     * 分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
        List<Category> categoryList =  categoryMapper.selectByNameAndType(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());
        PageInfo<Category> pageInfo = PageInfo.of(categoryList);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getList());
        return pageResult;
    }

    /**
     * 新增分类
     * @param categoryDTO
     */
    @Override
    public void save(CategoryDTO categoryDTO) {
        isSameName(categoryDTO);//验证名字是否一致
        isSortNull(categoryDTO.getSort());//sort不能为空且内容在0-99整型
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.insert(category);
    }

    /**
     * 根据id删除分类
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        categoryMapper.delete(id);
    }

    /**
     * 根据类型查询分类
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        List<Category> categoryList = categoryMapper.selectByType(type);
        return categoryList;
    }

    /**
     * 修改分类
     * @param categoryDTO
     */
    @Override
    public void update(CategoryDTO categoryDTO) {
        isSameName(categoryDTO);//不能修改名字和其他名字一样
        isSortNull(categoryDTO.getSort());//sort不能为空且内容在0-99整型
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.update(category);
    }

    /**
     * 修改状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Category category = categoryMapper.selectById(id);
        category.setStatus(status);
        categoryMapper.update(category);
    }


    /**
     * 查询所有分类(用户)
     *
     * @return {@link List}<{@link Setmeal}>
     */
    @Override
    public List<Category> allList() {
        List<Category> categoryList = categoryMapper.select();
        return categoryList;
    }

    //验证名字是否一致
    private void isSameName(CategoryDTO categoryDTO) {
        Integer categoryNameCount = categoryMapper.selectByNameNotSelf(categoryDTO.getName(),categoryDTO.getId());
        if(categoryNameCount>0){
            throw new CategoryCreateException(MessageConstant.REPEAT_CATEGORY_NAME);
        }
    }
    //sort不能为空且内容在0-99整型
    private void isSortNull(Integer sort){
        if(sort < 0 || sort > 99){
            throw new CategoryCreateException(MessageConstant.ILLEGAL_SORT_NUMBER);
        }
        if(Objects.isNull(sort)){
            throw new CategoryCreateException(MessageConstant.SORT_IS_NULL);
        }
    }
}
